package com.data.modules.message.handle.impl;

import com.alibaba.fastjson.JSONObject;
import com.data.common.api.vo.Result;
import com.data.common.constant.WebsocketConst;
import com.data.common.util.SpringContextUtils;
import com.data.modules.message.handle.ISendMsgHandle;
import com.data.modules.message.websocket.WebSocket;
import lombok.extern.slf4j.Slf4j;

import javax.websocket.Session;

/**
 * 自动部署
 */
@Slf4j
@SuppressWarnings("ALL")
public class CDSendMsgHandle implements ISendMsgHandle {

	@Override
	public void sendMsg(String id,String receiver, String title, String content) throws Exception {
		log.info("CD发消息模板=sendMsg==id:{}",id);
		try {
			WebSocket webSocket = SpringContextUtils.getBean(WebSocket.class);
			Result<String> result = new Result<String>();
			String userId = receiver;
			JSONObject obj = new JSONObject();
			obj.put(WebsocketConst.MSG_CMD, WebsocketConst.CMD_CD);
			obj.put(WebsocketConst.MSG_USER_ID, userId);
			obj.put(WebsocketConst.MSG_ID, id);
			obj.put(WebsocketConst.MSG_TXT, content);
			Session session = WebSocket.sessionPool.get(userId);
			if (session == null) {
				log.error(userId+" CD send Error session is down");
			}
			webSocket.sendMessage(userId, obj.toJSONString());
			result.setResult("单发");
			log.info(" CD msg success json = {}",obj.toJSONString());
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			throw new Exception(e.getMessage());
		}
	}

}
